package com.shiguiwu.springmybatis.javabase.nio.socket;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @description: socket
 * @author: stone
 * @date: Created by 2021/11/8 12:30
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.nio.socket
 */
public class SocketTests {

    public static void main(String[] args) throws Exception {
        //创建一个socket 通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));

        //设置非阻塞
        socketChannel.configureBlocking(false);

        //读操作
        ByteBuffer buffer = ByteBuffer.allocate(64);

        int read = 0;
        while ((read =  socketChannel.read(buffer)) != -1) {
            System.out.println("读取了：" + read);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.println((char) buffer.get());
            }
            buffer.clear();
        }
        socketChannel.close();

        System.out.println("read over");

    }
}
